package collection3;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * TreeMap的使用
 */
public class Demo03 {
    public static void main(String[] args) {
        //创建集合
        //因为是存储结构是红黑树需要比较  cannot be cast to java.lang.Comparable 需要 1.重写Comparable接口或者2.改造器匿名内部类
//        TreeMap<Student,String> treeMap  = new TreeMap<Student ,String>(new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                int n1 = o1.getName().compareTo(o2.getName());
//                int n2 =  o1.getStuNo() - o2.getStuNo();
//                return n2 == 0 ? n1:n2;
//            }
//        });
        TreeMap<Student,String> treeMap = new TreeMap<Student,String>();
        Student s1 =  new Student("curry",200);
        Student s2 =  new Student("James",6);
        Student s3 =  new Student("kobe",24);
        Student s4 =  new Student("zff",47);
        treeMap.put(s1,"金州");
        treeMap.put(s2,"Los");
        treeMap.put(s3,"Los");
        treeMap.put(s4,"厦门");
        System.out.println("元素个数：" + treeMap.size());
        System.out.println(treeMap.toString());
        //删除元素
        treeMap.remove(s1);
        //遍历元素
        System.out.println("=========使用keySet遍历元素=======");
        for (Student key : treeMap.keySet()) {
            System.out.println( key + treeMap.get(key));
        }
        System.out.println("=========使用entrySet遍历元素=======");
        for (Map.Entry<Student, String> entry : treeMap.entrySet()) {
            System.out.println(entry.getKey() + entry.getValue());
        }
        //4.判断
        System.out.println(treeMap.containsKey(s1));
        System.out.println(treeMap.containsKey(s4));
    }
}
